#include <QGridLayout>
#include <QKeyEvent>
#include <QLabel>
#include <QSpacerItem>
#include <QStackedLayout>
#include <QStackedWidget>

//#include <QSinWaveGenerator>

#include "qbciglinterface.h"
#include "qbcitarget.h"

QBciGLInterface::QBciGLInterface(QWidget *parent)
    : QOpenGLWidget(parent), Targets(new QBciTargetsGroup(this)) {
  connect(Targets, &QBciTargetsGroup::update, this,
          QOverload<>::of(&QWidget::update));

  // default Full Screen
  this->showFullScreen();

  // Background black
  QPalette plt = this->palette();
  plt.setColor(QPalette::Window, Qt::black);
  this->setPalette(plt);
  this->setAutoFillBackground(true);
}

QBciGLInterface::~QBciGLInterface() {}

void QBciGLInterface::paintEvent(QPaintEvent *) {
  QPainter painter;
  painter.begin(this);
  painter.setRenderHint(QPainter::Antialiasing, true);
  Targets->paint(&painter);
  painter.end();
}

void QBciGLInterface::keyReleaseEvent(QKeyEvent *event) {
  static bool freqEnableFlag = false;
  if (event->key() == Qt::Key_Escape) {
    if (isFullScreen) {
      this->showNormal();
    } else {
      this->showFullScreen();
    }
    isFullScreen = !isFullScreen;
  }
}
void QBciGLInterface::init() {
  targetsInit();

  // 60Hz refresh rate
  connect(&mFreshTimer, &QTimer::timeout, this,
          QOverload<>::of(&QWidget::update));
  mFreshTimer.start(1000 / 60);

  Targets->setEnableWave(true);
  // Init globally for WaveGenerators
  QWaveGeneratorInterface::init();
}
